/* eslint-disable max-classes-per-file */
/*
 * @Description:上传文件/excel导入
如果import 则上传路径默认为 web/upload/[dbName]/temp下 file  fs/file/[dbName] image fs/image/[dbName]
 * @Version: 2.0
 * @Autor: wiz
 * @Date: 2023-12-27 13:23:23
 * @LastEditors: swc
 * @LastEditTime: 2024-01-10 16:47:27
 */
import lexicon from '@/lib/lexicon';

interface I_interlinkChildren {
  funName: string;
  fieldName: string;
  children: Array<I_interlinkChildren>;
}
export class T_uploadArg {
  private url: string = lexicon.url.upload;

  protected wsCliID: string;

  protected routeID: string;

  protected type: 'import' | 'file' | 'image';

  // 导入excel有效
  protected funInfo?: Array<{ funName: string; fileName: string }>;

  // 导入excel有效
  protected dataHandle?: 'insert' | 'update';

  // 导入excel有效 同步导入写表 只上传文件
  protected option?: 'import' | 'uploadOnly';

  // 严格模式：传入的字段严格匹配一一对应。
  // 匹配模式: 有一个写一个，只要数据库不报错。
  protected mode?: 'strict' | 'match';

  // FileList
  private files: File[] = [];

  // 导入excel有效
  protected interlink?: {
    funName: string;
    children: I_interlinkChildren;
  };

  constructor(wsCliID: string) {
    this.funInfo = undefined;
    this.dataHandle = undefined;
    this.option = undefined;
    this.interlink = undefined;
    this.wsCliID = wsCliID;
  }

  public setType(p: 'import' | 'file' | 'image'): T_uploadArg {
    this.type = p;
    return this;
  }

  public getType(): 'import' | 'file' | 'image' {
    return this.type;
  }

  public setMode(p: 'strict' | 'match'): T_uploadArg {
    this.mode = p;
    return this;
  }

  public getMode(): 'strict' | 'match' {
    return this.mode;
  }

  public addFunInfo(
    ...p: { funName: string; fileName: string }[]
  ): T_uploadArg {
    if (!Array.isArray(this.funInfo)) {
      this.funInfo = [];
    }
    this.funInfo.push(...p);
    return this;
  }

  public getFunInfo(): Array<{ funName: string; fileName: string }> {
    return this.funInfo;
  }

  public setDataHandle(p: 'insert' | 'update'): T_uploadArg {
    this.dataHandle = p;
    return this;
  }

  public getDataHandle(): 'insert' | 'update' {
    return this.dataHandle;
  }

  public setOption(p: 'import' | 'uploadOnly'): T_uploadArg {
    this.option = p;
    return this;
  }

  public getOption(): 'import' | 'uploadOnly' {
    return this.option;
  }

  public setInterlink(p: {
    funName: string;
    children: I_interlinkChildren;
  }): T_uploadArg {
    this.interlink = p;
    return this;
  }

  public getInterlink(): {
    funName: string;
    children: I_interlinkChildren;
  } {
    return this.interlink;
  }

  public setFiles(p: File[]): T_uploadArg {
    this.files = p;
    return this;
  }

  public addFile(p: File): T_uploadArg {
    this.files.push(p);
    return this;
  }

  public getFiles(): File[] {
    return this.files;
  }

  public getWsCliID(): string {
    return this.wsCliID;
  }

  public getRouteID(): string {
    return this.routeID;
  }

  public SetRouteID(p: string): T_uploadArg {
    this.routeID = p;
    return this;
  }

  public chkParams(): boolean {
    if (!this.type) {
      console.error('params [type] is empty');
      return false;
    }
    if (this.type === 'import') {
      return (
        Array.isArray(this.funInfo) &&
        this.funInfo.length > 0 &&
        this.routeID &&
        (this.dataHandle === 'insert' || this.dataHandle === 'update') &&
        (this.option === 'import' || this.option === 'uploadOnly') &&
        (this.mode === 'strict' || this.mode === 'match')
      );
    }
    return true;
  }
}
export interface I_uploadArg extends T_uploadArg {}
